package com.example.xjtuse_pc.mobilesafe;

import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.example.xjtuse_pc.receiver.MyAdmin;

public class IndexActivity extends Activity {
    private DevicePolicyManager dpm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        dpm= (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
        //激活的管理员对象
        ComponentName mDeviceAdminSample =new ComponentName(IndexActivity.this, MyAdmin.class);
        //检查是否有管理员权限
        if(dpm.isAdminActive(mDeviceAdminSample)){
            Intent intent=new Intent(IndexActivity.this,SplashActivity.class);
            startActivity(intent);
            finish();
        }else{
            Log.i("join","1");
            StartAdmin();
            Log.i("join", "2");
            finish();

        }
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    /**
     * 开启管理员权限
     */
    public void StartAdmin() {
        //开启管理者权限
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        //激活的对象
        ComponentName mDeviceAdminSample =new ComponentName(this, MyAdmin.class);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample);
        //激活介绍页面
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"此软件需要激活管理员权限方可使用");
        startActivity(intent);
    }
}
